package com.pearadmin.nucleus.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.pearadmin.nucleus.shiro.realm.ShiroRealm;
import com.pearadmin.nucleus.shiro.session.ShiroSessionManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    /**
     * 配置权限验证
     * */
    @Bean
    public ShiroRealm shiroRealm(){

        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return shiroRealm;
    }

    /**
     * 配置缓存验证器
     * */
    @Bean
    public CacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }

    @Bean
    public ShiroSessionManager shiroSessionManager(){
        return new ShiroSessionManager();
    }

    @Bean
    public SecurityManager securityManager(@Qualifier("shiroRealm")ShiroRealm shiroRealm){

        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        defaultWebSecurityManager.setRealm(shiroRealm);

        defaultWebSecurityManager.setCacheManager(cacheManager());

        defaultWebSecurityManager.setSessionManager(shiroSessionManager());

        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") SecurityManager securityManager){

        ShiroFilterFactoryBean shiroFactory = new ShiroFilterFactoryBean();
        shiroFactory.setSecurityManager(securityManager);
        shiroFactory.setLoginUrl("/login");

        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("/static/**","anon");
        linkedHashMap.put("/admin/**","anon");
        linkedHashMap.put("/component/**","anon");
        linkedHashMap.put("/login","anon");
        linkedHashMap.put("/logout","anon");
        linkedHashMap.put("/**", "authc");

        shiroFactory.setFilterChainDefinitionMap(linkedHashMap);

        return shiroFactory;

    }

    /**
     * 配置shiro的生命周期
     * */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){

        return new LifecycleBeanPostProcessor();
    }

    /**
     * 配置Shiro注解是否生效
     * */
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    /**
     * 加密算法
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");//采用MD5 进行加密
        hashedCredentialsMatcher.setHashIterations(1);//加密次数
        return hashedCredentialsMatcher;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor sourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        sourceAdvisor.setSecurityManager(securityManager);
        return sourceAdvisor;
    }

    /**
     * 配置前端标签解析
     * */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

}
